layui.use(["layer","laydate","form","jquery","table"],function () {
    var
        layer = layui.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        table = layui.table,
        form = layui.form;
//全部已受理订单
    $("#selectAllAcceptedOrder").click(function () {
        var param = {
            "priceTotal":$("#priceTotal").val(),
            "paymentTime":$("#paymentTime").val(),
            "orderPaymentChannel":$("#orderPaymentChannel").val(),
            "orderSource":$("#orderSource").val(),
            "orderStatus":"9",
            "orderStatus2":"11"
        };
        $("#selectAllAcceptedOrder").css("backgroundColor","#009688");
        $("#selectAllAcceptedOrder").css("color","#FFFFFF");
        $("#AllAcceptedOrder").css("color","#FFFFFF");

        $("#selectCourseAcceptedOrder").css("backgroundColor","white");
        $("#selectCourseAcceptedOrder").css("color","green");
        $("#CourseAcceptedOrder").css("color","green");

        $("#selectProductAcceptedOrder").css("backgroundColor","white");
        $("#selectProductAcceptedOrder").css("color","green");
        $("#ProductAcceptedOrder").css("color","green");
        GetAllAcceptedOrders(param);
    });
    //课程已受理按钮
    $("#selectCourseAcceptedOrder").click(function () {
        var param = {
            "orderType":"2",
            "orderStatus":9,
            "orderStatus2":"11"
        }
        $("#selectCourseAcceptedOrder").css("backgroundColor","#009688");
        $("#selectCourseAcceptedOrder").css("color","#FFFFFF");
        $("#CourseAcceptedOrder").css("color","#FFFFFF");

        $("#selectAllAcceptedOrder").css("backgroundColor","white");
        $("#selectAllAcceptedOrder").css("color","green");
        $("#AllAcceptedOrder").css("color","green");

        $("#selectProductAcceptedOrder").css("backgroundColor","white");
        $("#selectProductAcceptedOrder").css("color","green");
        $("#ProductAcceptedOrder").css("color","green");
        //已受理订单
        QueryAcceptedOrdersByType(param);
    });
    //商品已受理按钮
    $("#selectProductAcceptedOrder").click(function () {
        var param = {
            "orderType":"3",
            "orderStatus":9,
            "orderStatus2":"11"
        }
        $("#selectProductAcceptedOrder").css("backgroundColor","#009688");
        $("#selectProductAcceptedOrder").css("color","#FFFFFF");
        $("#ProductAcceptedOrder").css("color","#FFFFFF");

        $("#selectAllAcceptedOrder").css("backgroundColor","white");
        $("#selectAllAcceptedOrder").css("color","green");
        $("#AllAcceptedOrder").css("color","green");

        $("#selectCourseAcceptedOrder").css("backgroundColor","white");
        $("#selectCourseAcceptedOrder").css("color","green");
        $("#CourseAcceptedOrder").css("color","green");
        //已受理订单
        QueryAcceptedOrdersByType(param);
    });
    //根据不同按钮查询不同状态信息
    function QueryAcceptedOrdersByType(param) {
        $.ajax({
            url:"/Order/QueryAcceptedOrdersByType",
            data:param,
            type:'post',
            dataType:'json',
            success:function (data) {
                showOrder(data.data);
            }
        })
    }
    var param = {
        "priceTotal":$("#priceTotal").val(),
        "paymentTime":$("#paymentTime").val(),
        "orderPaymentChannel":$("#orderPaymentChannel").val(),
        "orderSource":$("#orderSource").val(),
        "orderStatus":"9",
        "orderStatus2":"11"
    };
    //查询全部退订订单
    $.ajax({
        url:"/Order/AcceptedOrdersCount",
        data:param,
        type:'post',
        dataType:'json',
        success:function (data) {
            //CourseAcceptedOrder 已受理课程
            $("#CourseAcceptedOrder").text("("+data.data.CourseAcceptedOrders+")");
            //CancelTransactionProduct 已受理商品
            $("#ProductAcceptedOrder").text("("+data.data.ProductAcceptedOrders+")");
            var zong = Number(data.data.CourseAcceptedOrders)+Number(data.data.ProductAcceptedOrders);
            $("#AllAcceptedOrder").text("("+zong+")");
        }
    });
    GetAllAcceptedOrders(param);
$("#refresh").click(function () {
    location.reload();
});
    //绑定时间输入框
    laydate.render({
        elem:"#paymentTime",
        type:"datetime"
    })
    //点击搜索查询数据
    $("#search").click(function () {
        var param = {
            "priceTotal":$("#priceTotal").val(),
            "paymentTime":$("#paymentTime").val(),
            "orderPaymentChannel":$("#orderPaymentChannel").val(),
            "orderSource":$("#orderSource").val(),
            "orderStatus":"9",
            "orderStatus2":"11"
        };
        GetAllAcceptedOrders(param);
    });
    //初始化调用、查询所有已受理订单信息

    function GetAllAcceptedOrders(param){
        $.ajax({
            url:'/Order/GetAllAcceptedOrders',
            data:param,
            type:'post',
            dataType:'json',
            success:function(data){
                //调用table展示数据
                showOrder(data.data);
            }
        })
    }
    //重置
    $("#Reset").click(function () {
        $("#priceTotal").val(""),
            $("#paymentTime").val(""),
            $("#orderPaymentChannel").val(0),
            $("#orderSource").val(0)
        form.render();
        GetAllAcceptedOrders(param);
    });
    //高级检索
    $("#AdvancedRetrieval").click(function () {
        layer.open({
            title:"高级检索",
            type:2,
            content:"../courseOrder/courseOrderAdvancedRetrieval.html",
            area:["800px","500px"],
            btn:["确定","取消"],
            yes:function (index, layero) {
                var obj = layero.find('iframe').contents();
                var courseName = obj.find("input[name=courseName]").val();
                var nickname = obj.find("input[name=nickname]").val();
                var paymentTime = obj.find("input[name=paymentTime]").val();
                var deliveryTime = obj.find("input[name=deliveryTime]").val();
                var orderPaymentChannel = obj.find("select[name=orderPaymentChannel]").val();
                var orderSource = obj.find("select[name=orderSource]").val();
                var param = {
                    "courseName":courseName,
                    "nickname":nickname,
                    "paymentTime":paymentTime,
                    "deliveryTime":deliveryTime,
                    "orderPaymentChannel":orderPaymentChannel,
                    "orderSource":orderSource,
                    "orderStatus":"9",
                    "orderStatus2":"11"
                };
                console.log(param);

                $.ajax({
                    url:"/Order/AcceptedOrdersAdvancedRetrieval",
                    data:param,
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        showOrder(data.data);
                    }
                })
                layer.close(index);
            },btn2:function (index, layero) {

            }
        });
    });
    //table表格
    function showOrder(data){
        table.render({
            elem:"#tableTest",
            data:data,
            // width:1650,
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
                ,first: true //不显示首页
                ,last: true //不显示尾页
            },
            cols: [[
                {type:'checkbox'}
                ,{field:'nickname', title: '用户昵称',align:"center"}
                ,{field:'paymentTime',  title: '付款时间',align:"center", sort: true}
                ,{field:'deliveryTime', title: '发货时间',align:"center", sort: true}
                ,{field:'courseName', title: '课程名称', align:"center"}
                ,{field:'orderPaymentChannel', title: '支付方式',align:"center",templet:function (data) {
                        if (data.orderPaymentChannel==2){
                            return "微信支付";
                        }else if(data.orderPaymentChannel==3){
                            return "支付宝支付";
                        }
                    }}
                ,{field:'orderSource', title: '订单来源', align:"center",templet:function (data) {
                        if (data.orderSource == 2){
                            return "小程序";
                        }else if(data.orderSource == 3){
                            return "APP";
                        }
                    }} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                ,{field:'orderStatus', title: '订单状态',align:"center",templet:function (data) {
                        //2未付款，3已付款，4待发货，5已发货，6已签收，7退货申请，9已退货，10拒绝退订，11退货中
                        if (data.orderStatus == 2) {
                            return "未付款";
                        }else if(data.orderStatus == 3){
                            return "已付款";
                        }else if(data.orderStatus == 4){
                            return "待发货";
                        }else if(data.orderStatus == 5){
                            return "已发货";
                        }else if(data.orderStatus == 6){
                            return "已签收";
                        }else if(data.orderStatus == 7){
                            return "退货申请";
                        }else if(data.orderStatus == 9){
                            return "已退货";
                        }else if(data.orderStatus == 10){
                            return "拒绝退订";
                        }else if(data.orderStatus == 11){
                            return "退货中";
                        }
                    }}
                ,{field:'priceTotal', title: '商品总价',sort: true,align:"center"}
                ,{field:'receivingAddress', title: '收获地址',align:"center"}
                ,{field:'logisticsFee', title: '订单金额', sort: true,align:"center"}
                ,{field:'', title: '操作',align:"center",toolbar:"#rowFunction"}
            ]]
        });
        //行内工具栏监听事件
        table.on('tool(tableTest)',function (obj) {
            var objData = obj.data;
            var event = obj.event;
            if (event == "rowSelect") {
                layer.open({
                    title:"商品订单查看",
                    type:2,
                    content:"../courseOrder/courseOrderSelect.html",
                    area:["95%","100%"],
                    btn:["确定","取消"],
                    success:function(layero,index){
                        var obj = layero.find('iframe').contents();
                        obj.find("th[name=courseName]").text(objData.courseName);
                        obj.find("th[name=nickname]").text(objData.nickname);
                        if (objData.orderStatus==2){
                            obj.find("th[name=orderStatus]").text("未付款");
                        } else if (objData.orderStatus==3){
                            obj.find("th[name=orderStatus]").text("已付款");
                        }else if (objData.orderStatus==4){
                            obj.find("th[name=orderStatus]").text("待发货");
                        }else if (objData.orderStatus==5){
                            obj.find("th[name=orderStatus]").text("已发货");
                        }else if (objData.orderStatus==6){
                            obj.find("th[name=orderStatus]").text("已签收");
                        }else if (objData.orderStatus==7){
                            obj.find("th[name=orderStatus]").text("退货申请");
                        }else if (objData.orderStatus==9){
                            obj.find("th[name=orderStatus]").text("已退货");
                        }else if (objData.orderStatus==10){
                            obj.find("th[name=orderStatus]").text("拒绝退订");
                        }else if (objData.orderStatus==11){
                            obj.find("th[name=orderStatus]").text("退货中");
                        }


                        obj.find("th[name=orderLogisticsNumber]").text(objData.orderLogisticsNumber);
                        if (objData.orderPaymentChannel == 2){
                            obj.find("th[name=orderPaymentChannel]").text("微信支付");
                        }else if (objData.orderPaymentChannel == 3){
                            obj.find("th[name=orderPaymentChannel]").text("支付宝支付");
                        }
                        obj.find("th[name=receivingAddress]").text(objData.receivingAddress);
                        obj.find("th[name=paymentTime]").text(objData.paymentTime);
                        obj.find("th[name=deliveryTime]").text(objData.deliveryTime);
                        if(objData.orderSource == 2){
                            obj.find("th[name=orderSource]").text("小程序");
                        }else if (objData.orderSource == 3){
                            obj.find("th[name=orderSource]").text("APP");
                        }
                        obj.find("th[name=remarks]").text(objData.remarks);
                    },
                    yes:function (index,layero) {

                        layer.close(index);

                    },btn2:function (index,layero) {

                    }
                })

            }else if(event == "rowDelete"){
                layer.confirm("确定删除当前订单吗？",{icon:'3',title:"删除"},function (index) {
                    $.ajax({
                        url:"/Order/DeleteOrderById",
                        data:{"orderId":objData.orderId},
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            if(data.data == 1){
                                layer.msg("删除成功");
                                layer.close(index);
                            }else{
                                layer.msg("删除失败");
                                layer.close(index);
                            }
                        }
                    })
                })
            }
        });

    }
})